package com.sensePlusVR.activity.mine;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.bean.FeedbackItem;
import com.sensePlusVR.main.R;
import com.sensePlusVR.net.NetRequestManager;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.EditTextUtils;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MyToast;
import com.sensePlusVR.utils.StringUtils;
import com.sensePlusVR.utils.UserUtil;
import com.sensePlusVR.utils.VersionUtil;

/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:42:44
 * @desc: FeedbackActivity
 * 
 */
@ContentView(R.layout.activity_setting_core_feedback)
public class FeedbackActivity extends BaseActivity {

	private static final String TAG = FeedbackActivity.class.getSimpleName();
	@ViewInject(R.id.title_bar_title_tv)
	private TextView title_bar_title_tv;

	@ViewInject(R.id.feedback_email)
	private EditText feedback_email;

	@ViewInject(R.id.feedback_content)
	private EditText feedback_content;

	@ViewInject(R.id.feedback_store)
	private TextView feedback_store;
	
	private String userName;
	

	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}

	@Override
	public void initView() {
		super.initView();
		AppManager.getInstance().addActivity(this);

		userName = UserUtil.getLoginNameUser(FeedbackActivity.this);
		feedback_email.setText(userName);
		feedback_email.setSelection(feedback_email.getText().length());

		title_bar_title_tv.setText("Feedback");
//		feedback_store.setText(Html.fromHtml("<u>"
//				+ getResources().getString(R.string.feedkback_store) + "</u>"));// 下划线
	}

	@Event(value = { R.id.title_bar_left_ib, R.id.feedback_submit,
			R.id.feedback_store })
	private void setViewOnClick(View view) {
		switch (view.getId()) {
		case R.id.title_bar_left_ib:
			AppManager.getInstance().finishActivity(this);
			break;
		case R.id.feedback_submit:
			checkEmailFeedback();
			break;
		case R.id.feedback_store:
			MyPageJump.pageJump(this, StoreActivity.class);
			break;
		}
	}

	private void checkEmailFeedback() {
		if (checkInputData()) {
			String email = EditTextUtils.getText(feedback_email);
			String content = EditTextUtils.getText(feedback_content);
			String version = VersionUtil.getVersionName(this);
			if(version.length() > 1){
				int index = version.indexOf("v");
				if(index != -1){
					MyLog.d(TAG, "version 1 : " + version);
					version = version.substring(index+1);
					MyLog.d(TAG, "version 2 : " + version);
				}
			}
			FeedbackItem feedbackIem = new FeedbackItem(email.trim(), content,version);
			NetRequestManager.startFeedback(FeedbackActivity.this, feedbackIem,feedback_content);
		}
	}

	private boolean checkInputData() {
		userName = EditTextUtils.getText(feedback_email).trim();
		if (StringUtils.isEmpty(EditTextUtils.getText(feedback_email))) {
			MyToast.showMessage(getApplicationContext(),
					R.string.msg_common_e_null_email);
			return false;
		}
        if (!StringUtils.isEmail(userName)) {
        	MyToast.showMessage(getApplicationContext(),
                    R.string.msg_common_e_phone_number_email);
            return false;
        }
		if (StringUtils.isEmpty(EditTextUtils.getText(feedback_content))) {
			MyToast.showMessage(getApplicationContext(),
					R.string.msg_common_e_null_feedback);
			return false;
		}
		return true;
	}

}
